<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use App\Http\Resources\LogResource;
use App\Models\Log;
use Illuminate\Http\Request;

class LogController extends Controller
{

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $keyword = $request->get('keyword', '');
        $query = Log::query()->with('users');
        $models = $query->when($keyword, function ($queryString) use ($keyword) {
            return $queryString->where('handlers','like', '%' . $keyword . '%')
                ->orWhereHas('users',function ($q)use($keyword){
                 return $q->where('type','like', '%' . $keyword . '%');
            });
        })
           ->orderByDesc('id')->paginate();
        return LogResource::collection($models);
    }

}
